<?php

class HomeController extends BaseController {
    public function showWelcome() {
        return View::make('hello');
    }
    
    public function getTest(){ 
        $x = 'hello';
        return View::make('test')->with('vidu', $x);
    }

    public static function showTB($quyen, $id_bo_mon, $id_nguoi_dung) {
        $id_quyen = "";
        switch ($quyen) {
            case 'sv': $id_quyen = "4";
                break;
            case 'cb': $id_quyen = "3";
                break;
            case 'tbm': $id_quyen = "2";
                break;
            case 'qtht': $id_quyen = "1";
                break;
        }

        $array_chuoi_id_quyen = ThongBao::select("nhom_nguoi_xem", "noi_dung_tb")
                ->where("hien_thi", 1, "and")
                ->where("id_bo_mon", $id_bo_mon)
                ->where("ngay_bat_dau", "<=", date("Y-m-d", time()), "and")
                ->where("ngay_ket_thuc", ">=", date("Y-m-d", time()))
                ->orderBy("ngay_ket_thuc")
                ->get();
        
        $thong_bao = array();
        $i = 0;
        foreach ($array_chuoi_id_quyen as $chuoi_id_quyen) {
            $array_id_quyen = explode(",", $chuoi_id_quyen->nhom_nguoi_xem);
            if (Login::recursive_array_search($id_quyen, $array_id_quyen)) {
                $thong_bao[$i++] = $chuoi_id_quyen->noi_dung_tb;
            }
        }
        
        if ($id_quyen == '3') {
            $tbs = HoiDongXetDuyet::select("hoi_dong_xet_duyet.ma_so_hoi_dong", "hoi_dong_xet_duyet.thoi_gian_xet_duyet", "de_tai_luan_van.ten_de_tai", "chi_tiet_hoi_dong.mo_ta_vai_tro")
                    ->join('chi_tiet_hoi_dong', function($join) {
                                $join->on('chi_tiet_hoi_dong.id_hoi_dong', '=', 'hoi_dong_xet_duyet.id');
                            })
                    ->join('de_tai_luan_van', function($join) {
                                $join->on('hoi_dong_xet_duyet.id_de_tai', '=', 'de_tai_luan_van.id');
                            })
                    ->where('chi_tiet_hoi_dong.id_can_bo', $id_nguoi_dung, 'and')
                    ->where('hoi_dong_xet_duyet.thoi_gian_xet_duyet', '>=', date("Y-m-d H:i:s", time()))
                    ->orderBy('hoi_dong_xet_duyet.thoi_gian_xet_duyet')
                    ->get();
            foreach ($tbs as $tb) {
                $thong_bao[$i++] = "Há»™i Ä‘á»“ng " . $tb->ma_so_hoi_dong . " xÃ©t duyá»‡t Ä‘á» tÃ i " . $tb->ten_de_tai . " vá»›i vai trÃ² " . $tb->mo_ta_vai_tro . " vÃ o lÃºc " . date("H:i d/m/Y", strtotime($tb->thoi_gian_xet_duyet));
            }
        }
        return $thong_bao;
    }
    
    public function nienKhoa(){
        $nien_khoa = HocKyNienKhoa::select('nien_khoa')
                        ->distinct()
                        ->orderBy('id','desc')
                        ->get();
        return $nien_khoa;
    }
  


    public function getHknkCurrent(){
        $hknk =  HocKyNienKhoa::select('nien_khoa','hoc_ky')
            ->where('id',HocKyNienKhoa::max('id'))
            ->get();
        return $hknk;
    }
    public function shownienkhoahockyhientai()
    { 
        $array_namhoc = DB::table('hoc_ky_nien_khoa')
            ->select('nien_khoa','hoc_ky')
            ->distinct()
            ->orderBy('nien_khoa','desc')
            ->first();
        return ($array_namhoc);                 
    } 

    
    public static function getIdHknkMax()
    {
        return HocKyNienKhoa::max('id');
    }
    
    public static function checkInTime($linkmenu)
    {
        $result = DB::select(DB::raw("call kiem_tra_tg_tac_vu('" . $linkmenu . "');"));
        if(count($result) !=0 || true)
        {
        return true;
        }
        else{
            return false;
        }
    }
    
}

